import {
	defineStore
} from 'pinia'
import {
	getHomeMutidata,
	getTabItemsData,
} from '@/service/home.js'

// 使 tab-control 更加通用
export const types = ['pop', 'new', 'sell']

function getDefaultGoodsListData() {
	let goodsListOrign = {}
	types.forEach((type) => {
		goodsListOrign[type] = {
			page: 0,
			list: []
		}
	})
	return goodsListOrign
}

export const useHomeStore = defineStore("home", {
	state: () => {
		return {
			banner: [],
			recommend: [],
			currentType: 'pop', // pop new sell
			// 注意这种存储数据的方式
			// goodLists: {
			// 	'pop': {
			// 		page: 0,
			// 		list: []
			// 	},
			// 	'new': {
			// 		page: 0,
			// 		list: []
			// 	},
			// 	'sell': {
			// 		page: 0,
			// 		list: []
			// 	}
			// }
			goodsList: getDefaultGoodsListData()
		}
	},
	actions: {
		// 获取首页轮播图和推荐栏的数据
		async fetchHomeMutilData() {
			const res = await getHomeMutidata()
			this.banner = res.data.banner.list
			this.recommend = res.data.recommend.list
		},
		// 获取选项卡下方的数据项
		async fetchTabItemsData(type, page) {
			const res = await getTabItemsData(type, page)
			console.log(res);
			this.goodLists[type].list.push(...res.data.list)
			this.goodLists[type].page = page
		},
		setCurrentType(type) {
			this.currentType = type
		},
	}
})